<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./demo.css">
    <title>自定义指令</title>
</head>
<style>

</style>

<body>
    <!--作业：结合模板引用和自定义指令，实现表单的验证和错误样式的展示（标记红色，错误信息提示）,再思考，怎么方便去获取表单的数据-->
<div id="main">
    <form action="">
        
        <div>用户名：<input type="text" ref="user"></div>
        <span v-show="userMis" v-err='errColor'>{{usernameErr}}</span>
        <div>密&nbsp;&nbsp;&nbsp;码：<input type="text" ref="password"></div>
        <span v-show="pwMis" v-err='errColor'>{{pwErr}}</span>
        <div><button @click.prevent.stop="sub" style="width: 50px;height:30px ;">Login</button></div>
        
    </form>
</div>
</body>
<script src="https://unpkg.com/vue@3.2.47/dist/vue.global.js"></script>
<script>
    let app = Vue.createApp({
        methods:{
            sub:function(){
                if(this.$refs.user.value !=this.user || this.$refs.user.value === ''){
                    this.userMis= true;
                }else if(this.$refs.user.value == this.user && this.$refs.password.value == this.password){
                    this.userMis = false;
                    this.pwMis = false;
                    alert('登陆成功');
                    window.location.href='./demo.html'
                }else{
                    this.userMis= false;
                    this.pwMis= true
                }
            }
        },
        data: function () {
            return {
                user: 'arashi',
                password: 123456,
                usernameErr: '用户名错误！',
                pwErr: '密码错误！',
                errColor: 'red',
                userMis: false,
                pwMis: false
            }
        },
        
        directives: {
            err:function(el,binding){
                let color = binding.value
                el.style.color = color
            },
        }
    }).mount("body");
</script>

</html>